﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoreProvince;
using CoreNhaDat;

namespace BatDongSan.Ucs
{
    public partial class ListNhaDatDetail : System.Web.UI.UserControl
    {
        int _id;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _id = Convert.ToInt32(Request.QueryString["id"]);
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
            catch { }
        }

        protected void LoadData()
        {
            try
            {
                int nhadatId = (int)PlaceBll.GetVGNhaDatById(_id, Utility.strConnect).NhaDatId;
                dtlNhaDatBan.DataSource = PlaceBll.GetList(nhadatId, Utility.strConnect);
                dtlNhaDatBan.DataBind();
                if (dtlNhaDatBan.Items.Count > 0)
                {
                    pnEmptyData.Visible = false;
                }
                else
                {
                    pnEmptyData.Visible = true;
                }
            }
            catch { }

        }

        protected void dtlNhaDatBan_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    VG_NhaDat info = (VG_NhaDat)e.Item.DataItem;


                    lbTitle.Text = NhaDatBll.GetNameById(Convert.ToInt32(info.NhaDatId), Utility.strConnect);

                    HyperLink hplImg = (HyperLink)e.Item.FindControl("hplImg");
                    string linkImg = string.Format("http://{0}/Uploads/NhaDat/{1}", Request.Url.Authority, info.Img);
                    if (cvk.AppCode.WebUtility.CheckImage(linkImg) && !string.IsNullOrEmpty(info.Img))
                    {
                        hplImg.ImageUrl = string.Format("/Uploads/NhaDat/{0}", info.Img);
                    }
                    else
                    {
                        hplImg.ImageUrl = string.Format("/Uploads/NhaDat/{0}", "noimage.png");
                    }

                    HyperLink hplTitle = (HyperLink)e.Item.FindControl("hplTitle");
                    hplTitle.Text = info.Name;

                    Label lbDateBegin = (Label)e.Item.FindControl("lbDateBegin");
                    lbDateBegin.Text = "Ngày đăng: " + string.Format("{0:dd/MM/yyyy}", info.DateBegin);

                    Label lbDescription = (Label)e.Item.FindControl("lbDescription");
                    lbDescription.Text = cvk.AppCode.WebUtility.CatString(info.Description, 40);

                    Label lbPrice = (Label)e.Item.FindControl("lbPrice");
                    if (info.Price > 0)
                        lbPrice.Text = String.Format("{0:0,0}", info.Price) + " " + info.PriceUnit;
                    else
                        lbPrice.Text = "Thương lượng";

                    Label lbArea = (Label)e.Item.FindControl("lbArea");
                    lbArea.Text = info.Area.ToString() + " " + info.AreaUnit;

                    Label lbProvince = (Label)e.Item.FindControl("lbProvince");
                    lbProvince.Text = WardBll.GetById((int)info.WardId, Utility.strConnect).Name + " , " + ProvinceBll.GetById((int)info.ProvinceId, Utility.strConnect).Name;
                    hplImg.NavigateUrl = hplTitle.NavigateUrl = "/NhaDatDetail.aspx?id=" + info.Id.ToString();
                }
            }
            catch { }
        }
    }
}